package com.sophia.menuorder;

import java.util.ArrayList;
import java.util.List;

import com.sophia.menuorder.model.Dish;
import com.sophia.menuorder.type.MenuTypeDetailFragment;

import android.app.ActionBar;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements OnClickListener, ActionBar.OnNavigationListener {
	
    private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
    public static SharedPreferences sharedPref1,sharedPref2,sharedPref3;
    public static List<Dish> orderedDishList = new ArrayList<Dish>();
    
    private Button btnEnsure, btnCancel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        
        btnEnsure = (Button) findViewById(R.id.btnEnsure);
        btnCancel = (Button) findViewById(R.id.btnCancel);

        // Set up the dropdown list navigation in the action bar.
        actionBar.setListNavigationCallbacks(
                // Specify a SpinnerAdapter to populate the dropdown list.
                new ArrayAdapter(
                        actionBar.getThemedContext(),
                        android.R.layout.simple_list_item_1,
                        android.R.id.text1,
                        new String[]{
                                getString(R.string.title_section1),
                                getString(R.string.title_section2),
                                getString(R.string.title_section3),
                        }),
                this);

		sharedPref1 = getSharedPreferences("orderState1", Context.MODE_PRIVATE);
		sharedPref2 = getSharedPreferences("orderState2", Context.MODE_PRIVATE);
		sharedPref3 = getSharedPreferences("orderState3", Context.MODE_PRIVATE);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
            getActionBar().setSelectedNavigationItem(
                    savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt(STATE_SELECTED_NAVIGATION_ITEM,
                getActionBar().getSelectedNavigationIndex());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    @Override
    public boolean onNavigationItemSelected(int position, long id) {
        // When the given tab is selected, show the tab contents in the container
        /*Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
        fragment.setArguments(args);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, fragment)
                .commit();*/
    	Fragment fragment;
    	if (position == 0) {
    		fragment = new MenuTypeDetailFragment();
            Bundle args = new Bundle();
            args.putInt("position", position);
            fragment.setArguments(args);
        	getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
		} else if (position == 1) {
    		fragment = new MenuTypeDetailFragment();
            Bundle args = new Bundle();
            args.putInt("position", position);
            fragment.setArguments(args);
        	getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
		} else if (position == 2) {
    		fragment = new MenuTypeDetailFragment();
            Bundle args = new Bundle();
            args.putInt("position", position);
            fragment.setArguments(args);
        	getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
		}
        return true;
    }

    @Override
    protected void onDestroy() {
    	super.onDestroy();
    	sharedPref1.edit().clear().commit();
    	sharedPref2.edit().clear().commit();
    	sharedPref3.edit().clear().commit();
    }

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.btnEnsure:
			
			break;

		case R.id.btnCancel:
			
			break;
			
		default:
			break;
		}
	}
}
